import { 
  getCurrentCompetition, 
  getAdvertisePicture, 
  deleteAdvertisePicture,
  getSignUpInfo,
  setFillItem,
  setSingleOption,
  setDoubleOption,
  deleteOption,
  getOrganizer,
  getContact,
  getCompetitionInformation,
  setOrganizer,
  setContact,
  setCompetitionInformation} from '@/services/competition'
import { compose } from 'redux';
import moment from 'moment';

export default {
  namespace: 'settings',

  state: {
    fillInItemList: [],
    singleLevelOptionList: [],
    doubleLevelOptionList: [],
    Organizer: {},
    Contact: {},
    CompetitionInformation: {},
  },

  effects: {
    *getCurrentCompetition({ payload, callback },{ call, put }){
      const response = yield call(getCurrentCompetition)
      callback&&callback(response.data)
    },
    *getAdvertisePicture({ payload, callback },{ call, put }){
      const response = yield call(getAdvertisePicture, payload)
      callback&&callback(response.data)
    },
    *deleteAdvertisePicture({ payload, callback },{ call, put }){
      const response = yield call(deleteAdvertisePicture, payload)
      callback&&callback(response.data)
    },
    *getSignUpInfo({ payload, callback },{ call, put }){
      const response = yield call(getSignUpInfo);
      // 填空
      const fillInItemList = response.data.fillInItem;
      // 单项选择
      const singleLevelOptionList = []
      // 双项选择
      const doubleLevelOptionList = []

      const singleLevelOptions = response.data.singleLevelOption
      const doubleLevelOptions = response.data.doubleLevelOption

      singleLevelOptions.forEach(element => {
        const singleLevelOption = element
        const option = element.option.split(",");
        singleLevelOption["option"] = option;
        singleLevelOptionList.push(singleLevelOption)
      });

      doubleLevelOptions.forEach(element => {
        const firstOption = element.firstOption.split(",")
        const secondOption = element.secondOption.split(":")
        const tempList = []
        const doubleLevelOption = {}
        for(var i=0;i<firstOption.length;i++){
          const option = {
            value: "",
            label: "",
            children: [],
          }
          option["value"] = firstOption[i]
          option["label"] = firstOption[i]
          const temp = secondOption[i].split(",")
          temp.forEach(element => {
            const e = {}
            e["value"] = element
            e["label"] = element
            option["children"].push(e)
          }) 
          tempList.push(option) 
        }
        doubleLevelOption["title"] = element.title
        doubleLevelOption["option"] = tempList

        doubleLevelOptionList.push(doubleLevelOption)
      });
      // console.log("doubleLevelOptionList",doubleLevelOptionList)
      // console.log("singleLevelOptionList",singleLevelOptionList)
      yield put({
        type: 'saveOptionList',
        payload:{
          fillInItemList,
          singleLevelOptionList,
          doubleLevelOptionList,
        },
      })
    },
    *setFillInItem({ payload, callback },{ call, put }){
      const response = yield call(setFillItem, payload)
      yield put({
        type: 'getSignUpInfo'
      })
      callback&&callback(response.data)
    },
    *setSingleOption({ payload, callback },{ call, put }){
      const response = yield call(setSingleOption, payload)
      yield put({
        type: 'getSignUpInfo'
      })
      callback&&callback(response.data)
    },
    *setDoubleOption({ payload, callback },{ call, put }){
      const option = payload.option;
      const payloadres = {
        "title" : payload.title,
        "type" : payload.type,
        "firstOptions" : "",
        "secondOptions" : "",
      }
      const temp = []
      const sectemp = []
      option.forEach(element => {
        if(element.firstOption){
          temp.push(element.firstOption)
          const str = element.secondOption.replace(/，/g, ",")
          sectemp.push(str)
        }
      });
      payloadres.firstOptions=temp.join(",")
      payloadres.secondOptions=sectemp.join(":")
      const response = yield call(setDoubleOption, payloadres)
      
      yield put({
        type: 'getSignUpInfo'
      })
      callback&&callback(response.data)
    },
    *deleteOption({ payload, callback },{ call, put }){
      const response = yield call(deleteOption, payload)
      yield put({
        type: 'getSignUpInfo'
      })
    },
    *getCompetitionInformation({ payload, callback },{ call, put }){
      const response = yield call(getCompetitionInformation)
      const CompetitionInformation = response.data
      if(response.data.competitionStartDate&&response.data.competitionEndDate){
        const competitionStartDate = [
        moment(response.data.competitionStartDate, "YYYY-MM-DD HH:mm:ss"), 
        moment(response.data.competitionEndDate, "YYYY-MM-DD HH:mm:ss")
        ]
        const enrollStartDate = [
          moment(response.data.enrollStartDate, "YYYY-MM-DD HH:mm:ss"), 
          moment(response.data.enrollEndDate, "YYYY-MM-DD HH:mm:ss")
        ]
        delete CompetitionInformation.competitionEndDate
        delete CompetitionInformation.enrollEndDate

        CompetitionInformation["competitionStartDate"] = competitionStartDate
        CompetitionInformation["enrollStartDate"] = enrollStartDate
      }
      

      // console.log(CompetitionInformation)

      yield put({
        type: 'saveCompetitionInformation',
        payload : {
          CompetitionInformation,
        },
      })
      callback(CompetitionInformation.competitionIntroduction)
    },
    *getContact({ payload, callback },{ call, put }){
      const response = yield call(getContact)
      const Contact = response.data
      yield put({
        type: 'saveContact',
        payload : {
          Contact
        }
      })
    },
    *getOrganizer({ payload, callback },{ call, put }){
      const response = yield call(getOrganizer)
      // console.log(response)
      yield put({
        type: 'saveOrganizer',
        payload : {
          Organizer: response.data
        }
      })
    },
    *setInformation({ payload, callback },{ call, put }){
      const competitionStartDate = payload.competitionStartDate[0].format("YYYY-MM-DD HH:mm:ss")
      const competitionEndDate = payload.competitionStartDate[1].format("YYYY-MM-DD HH:mm:ss")
      const enrollStartDate = payload.enrollStartDate[0].format("YYYY-MM-DD HH:mm:ss")
      const enrollEndDate = payload.enrollStartDate[1].format("YYYY-MM-DD HH:mm:ss")

      const Organizer = {
        "sponsor": payload.sponsor,
        "organizer": payload.organizer,
        "coOrganizer": payload.co_organizer,
      }
      const Contact = {
        "mail": payload.contactEmail,
        // "contactExplain": payload.contactExplain,
        "name": payload.contactName,
        "phone": payload.contactPhone,
        "qq": payload.contactQQ,
        "wechat": payload.contactWeChat,
      }
      const CompetitionInformation = {
        "competitionIntroduction": payload.competitionIntroduction,
        "competitionStartDate": competitionStartDate,
        "competitionEndDate": competitionEndDate,
        "name": payload.name,
        "enrollStartDate": enrollStartDate,
        "enrollEndDate": enrollEndDate,
      }

        const responseOrganizer = yield call(setOrganizer,Organizer)
        const responseContact = yield call(setContact,Contact)
        const responseCompetitionInformation = yield call(setCompetitionInformation,CompetitionInformation)
        
        if(responseOrganizer&&responseContact&&responseCompetitionInformation){
          callback("finsh")
        }
        callback("")
    }
  },

  reducers: {
    saveOptionList(state, { payload }){
      return {
        ...state,
        fillInItemList: payload.fillInItemList,
        singleLevelOptionList: payload.singleLevelOptionList,
        doubleLevelOptionList: payload.doubleLevelOptionList
      }
    },

    saveOrganizer(state, action ){
      // console.log(action)
      return {
        ...state, ...action.payload
      }
    },

    saveContact(state, { payload }){
      return {
        ...state,
        Contact: payload.Contact,
      }
    },

    saveCompetitionInformation(state, { payload }){
      return {
        ...state,
        CompetitionInformation: payload.CompetitionInformation,
      }
    },
    
  }
};
